import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import Base64 from "base-64";
import CryptoJS from "crypto-js";
function S4() {
    return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
}
export function guid() {
    return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
}
var accessid = "LTAI5t9ndsvDU6kEZLYBL4YW";
var accesskey = "GXw4FPI7VAhYAm3CExsAguBZSGOFVu";
var host = "https://twmw.oss-ap-southeast-1.aliyuncs.com";
var httpsHost = "https://twmw.oss-ap-southeast-1.aliyuncs.com";
var policyText = {
    expiration: "2030-01-01T12:00:00.000Z",
    conditions: [
        [
            "content-length-range",
            0,
            10485760000
        ]
    ]
};
var policy = Base64.encode(JSON.stringify(policyText));
var bytes = CryptoJS.HmacSHA1(policy, accesskey, {
    asBytes: true
});
var signature = bytes.toString(CryptoJS.enc.Base64);
function uploadAliyun(fileList) {
    return _uploadAliyun.apply(this, arguments);
}
function _uploadAliyun() {
    _uploadAliyun = _async_to_generator(function(fileList) {
        var uploadTasks;
        return _ts_generator(this, function(_state) {
            uploadTasks = fileList.map(function(file) {
                var name = file.name, _file_type = file.type, type = _file_type === void 0 ? "image/png" : _file_type;
                var suffixIndex = name.lastIndexOf(".");
                var fileName = name.slice(0, suffixIndex);
                var extname = name.slice(suffixIndex + 1);
                var nFileName = fileName;
                var key = "";
                if (nFileName) {
                    key += nFileName;
                    if (nFileName.lastIndexOf("/") !== nFileName.length - 1) {
                        key += "-";
                    }
                }
                key += "".concat(guid(), ".").concat(extname);
                if (file && file.url && typeof file.url === "string") {
                    return file.url;
                } else {
                    var form = new FormData();
                    form.append("name", "file");
                    form.append("signature", signature);
                    form.append("OSSAccessKeyId", accessid);
                    form.append("policy", policy);
                    form.append("key", "onawallet/" + key);
                    form.append("success_action_status", "200");
                    form.append("file", file);
                    return fetch(host, {
                        method: "post",
                        body: form,
                        mode: "cors"
                    }).then(function(param) {
                        var status = param.status;
                        if (status === 200) {
                            return "".concat(httpsHost, "/onawallet/").concat(key);
                        }
                        throw new Error("上传失败");
                    });
                }
            });
            return [
                2,
                Promise.all(uploadTasks)
            ];
        });
    });
    return _uploadAliyun.apply(this, arguments);
}
function upload(fileList) {
    return _upload.apply(this, arguments);
}
function _upload() {
    _upload = _async_to_generator(function(fileList) {
        var res, e;
        return _ts_generator(this, function(_state) {
            switch(_state.label){
                case 0:
                    _state.trys.push([
                        0,
                        2,
                        ,
                        3
                    ]);
                    return [
                        4,
                        uploadAliyun(fileList)
                    ];
                case 1:
                    res = _state.sent();
                    return [
                        2,
                        res
                    ];
                case 2:
                    e = _state.sent();
                    console.error({
                        title: "阿里云上传失败",
                        icon: "none"
                    });
                    return [
                        2,
                        Promise.reject(e)
                    ];
                case 3:
                    return [
                        2
                    ];
            }
        });
    });
    return _upload.apply(this, arguments);
}
export function deleteOsFile(filename) {
    return _deleteOsFile.apply(this, arguments);
}
function _deleteOsFile() {
    _deleteOsFile = _async_to_generator(function(filename) {
        return _ts_generator(this, function(_state) {
            try {} catch (e) {
                return [
                    2,
                    Promise.reject(e)
                ];
            }
            return [
                2
            ];
        });
    });
    return _deleteOsFile.apply(this, arguments);
}
/** aliyun 裁剪 */ /** 几倍图 */ var multiple = 2;
/** 整数 */ function trunc(nu) {
    return Math.trunc(nu) * multiple;
}
/** 获取剪接图片后缀 */ export function getResizeUrl(param) {
    var width = param.width, height = param.height, model = param.model;
    var url = "?x-oss-process=image/resize,";
    url += model !== null && model !== void 0 ? model : "m_fill";
    if (width) {
        url += ",w_".concat(trunc(width));
    }
    if (height) {
        url += ",h_".concat(trunc(height));
    }
    return url;
}
/**
 * 获取视频第一帧图片
 *
 * @static
 * @param {{ width: number, height: number }} { width, height }
 * @returns
 * @memberof AliYun
 */ export function getVideoSnapshotUrl(param) {
    var width = param.width, height = param.height;
    var url = "?x-oss-process=video/snapshot,t_7000,f_jpg";
    if (width) {
        url += ",w_".concat(trunc(width));
    }
    if (height) {
        url += ",h_".concat(trunc(height));
    }
    return url;
}
export { upload };

 //# sourceMappingURL=aliyun.js.map